package com.mengye.control.userInfo;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mengye.bean.UserInfoBean;
import com.mengye.control.LoginControl;
import com.mengye.model.UserInfoModel;
import com.mengye.utils.MyUtils;

/**
 * Servlet implementation class UserInfoModifyControl
 */
@WebServlet("/UserInfoModifyControl")
public class UserInfoModifyControl extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String oldPsw = request.getParameter("oldPsw");
		String newPsw = request.getParameter("newPsw");
		System.out.println("old and new " +oldPsw+"  "+newPsw);
		UserInfoBean userInfoB = new UserInfoBean();
		userInfoB.setUser_Name(LoginControl.user_name);
		List<UserInfoBean> lst = MyUtils.convertRequest2BeanWithFileUpload(request, UserInfoModel.class).queryPsw(userInfoB);
		System.out.println(lst.get(0));
		System.out.println(lst.get(0).getUser_Psw().toString());
		if(lst.get(0).getUser_Psw().toString().equals(oldPsw)) {
			userInfoB.setUser_Psw(newPsw);
		}
		
		System.out.println(userInfoB.toString());
		int i = 0;
		i = MyUtils.convertRequest2BeanWithFileUpload(request, UserInfoModel.class).updateInfo(userInfoB);
		if(i>0) {
			response.getWriter().write("ok");
		}else {
			response.getWriter().write("no");
		}
		request.getRequestDispatcher("WEB-INF/logined.jsp").forward(request, response);
	}

}
